/**
 * Copyright (C)  2011  chen wei hua.
 * Permission is granted to copy, distribute and/or modify this document
 * under the terms of the GNU Free Documentation License, Version 2.0
 * or any later version published by the Free Software Foundation;
 * with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts.
 * A copy of the license is included in the section entitled "GNU Free Documentation License".
 */
package com.javaeye.common.util;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.javaeye.common.dto.Code;
import com.javaeye.common.service.ICodeService;

public class ListUtil {
	
	protected static Log log = LogFactory.getLog(ListUtil.class);
	
	/**
	 * 单位
	 */
	private static String UNIT_CODE_CATEGORY = "001";
	
	private static List<ListItem> codeCategoryItems;
	
	/**
	 * 订单状态
	 */
	private static List<ListItem> orderItems;
	
	/**
	 * 物流方式
	 */
	private static List<ListItem> logisticsTypeItems;
	
	/**
	 * 用户职务
	 */
	private static List<ListItem> userDutyItems;
	
	private static List<ListItem> checkResultItems;
	
	static {
		
		codeCategoryItems = new ArrayList<ListItem>();
		codeCategoryItems.add(new ListItem(UNIT_CODE_CATEGORY, "单位"));
		
		checkResultItems = new ArrayList<ListItem>();
		checkResultItems.add(new ListItem("1", "合格"));
		checkResultItems.add(new ListItem("2", "不合格"));
		checkResultItems.add(new ListItem("3", "宽放"));
		checkResultItems.add(new ListItem("4", "特采放行"));
		
		orderItems = new ArrayList<ListItem>();
		orderItems.add(new ListItem("1", "等待计划安排"));
		orderItems.add(new ListItem("2", "等待业务确认"));
		orderItems.add(new ListItem("3", "等待入库"));
		orderItems.add(new ListItem("4", "等待检验"));
		orderItems.add(new ListItem("5", "等待复检"));
		orderItems.add(new ListItem("6", "等待发货"));
		orderItems.add(new ListItem("7", "等待关闭"));
		
		logisticsTypeItems = new ArrayList<ListItem>();
		logisticsTypeItems.add(new ListItem("1", "公司合约货运"));
		logisticsTypeItems.add(new ListItem("2", "公司自用车辆"));
		logisticsTypeItems.add(new ListItem("3", "公司临时货运"));
		logisticsTypeItems.add(new ListItem("4", "客户自运"));
		logisticsTypeItems.add(new ListItem("5", "业务员提货"));
		logisticsTypeItems.add(new ListItem("6", "出口货柜"));
		logisticsTypeItems.add(new ListItem("7", "其他"));
		
		userDutyItems = new ArrayList<ListItem>();
		userDutyItems.add(new ListItem("1", "职员"));
		userDutyItems.add(new ListItem("2", "经理"));
		userDutyItems.add(new ListItem("3", "总经理"));
		
	}
	
	/**
	 * 取得常用数据分类列表
	 * @return
	 */
	public static List<ListItem> getCodeCategoryList() {
		return codeCategoryItems;
	}
	
	/**
	 * 取得所有的单位列表
	 * @return
	 */
	public static List<ListItem> unitList(ICodeService service) {
		//PCS ROLLS KGS m2
		List<ListItem> units = new ArrayList<ListItem>();
		List<Code> list = service.getCodesByCategoryId(UNIT_CODE_CATEGORY);
		for (Code c : list) {
			units.add(new ListItem(String.valueOf(c.getId()), c.getName()));
		}
		return units;
	}
	
	/**
	 * 取得Id对应的单位名称
	 * @return
	 */
	public static String unitName(ICodeService service, String id) {
		return service.getCodeName(id);
	}
	
	/**
	 * 取得所有的检验结果列表
	 * @return
	 */
	public static List<ListItem> checkResultList() {
		return checkResultItems;
	}
	
	/**
	 * 取得Id对应的检验结果名称
	 * @return
	 */
	public static String checkResultName(String id) {
		int index = Integer.parseInt(id) - 1;
		return getItemName(checkResultItems, index);
	}
	
	/**
	 * 取得所有的用户职务列表
	 * @return
	 */
	public static List<ListItem> userDutyList() {
		return userDutyItems;
	}
	
	/**
	 * 取得Id对应的用户职务名称
	 * @return
	 */
	public static String userDutyName(String id) {
		int index = Integer.parseInt(id) - 1;
		return getItemName(userDutyItems, index);
	}
	
	/**
	 * 取得所有的订单状态
	 * @return
	 */
	public static List<ListItem> orderStatusList() {
		return orderItems;
	}
	
	/**
	 * 取得Id对应的订单状态
	 * @return
	 */
	public static String orderStatusName(String id) {
		int index = Integer.parseInt(id) - 1;
		return getItemName(orderItems, index);
	}
	
	/**
	 * 取得所有的物流方式
	 * @return
	 */
	public static List<ListItem> logisticsTypeList() {
		return logisticsTypeItems;
	}
	
	/**
	 * 取得Id对应的物流方式
	 * @return
	 */
	public static String logisticsTypeName(String id) {
		//log.debug("取得Id(" + id + ")对应的物流方式");
		int index = Integer.parseInt(id) - 1;
		return getItemName(logisticsTypeItems, index);
	}
	
	private static String getItemName(List<ListItem> list, int index) {
		if (index >= list.size() || index < 0) {
			return "";
		}
		return list.get(index).getValue();
	}
	
}
